package com.kj.tms.system.service.basicinfo.serviceImpl;


import com.kj.tms.common.bean.dao.BaseMapper;
import com.kj.tms.common.bean.dto.Pager;
import com.kj.tms.common.bean.service.BaseService;
import com.kj.tms.common.utils.uuid.IdGen;
import com.kj.tms.pri.system.basicinfo.dto.BaseMessageDto;
import com.kj.tms.pri.system.basicinfo.dto.ShareholderDto;
import com.kj.tms.pri.system.basicinfo.service.BaseMessageService;
import com.kj.tms.system.service.basicinfo.dao.BaseMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangruidong on 2018/3/15.
 */
@Service
@Transactional
public class BaseMessageServiceImpl extends BaseService<ShareholderDto> implements BaseMessageService {
    @Autowired
    private BaseMessageMapper baseMessageMapper;

    @Override
    public BaseMapper<ShareholderDto> getMapper() {
        return baseMessageMapper;
    }

    /**
    * @Method      save
    * @Param       企业基础信息dto
    * @Return
    * @Exception   保存基业基础信息
    * @Description [方法描述]
    * @Author      yangruidong
    * @Version     1.0
    * @Date        2018/3/28 14:08
    */
    @Override
    public void save(@RequestBody BaseMessageDto dto) {
        baseMessageMapper.save(dto);
    }
    /**
     * @Method      update
     * @Param       企业基础信息dto
     * @Return
     * @Exception   更新基业基础信息
     * @Description [方法描述]
     * @Author      yangruidong
     * @Version     1.0
     * @Date        2018/3/28 14:08
     */
    @Override
    public void update(@RequestBody BaseMessageDto dto) {
        baseMessageMapper.update(dto);
    }

    /**
     * @Method [listByPager]
     * @Description [分页查询]
     * @Author [yangruidong]
     * @Version 1.0
     * @Param dto 封装得参数Dto
     * @Return
     * @Exception
     * @Date
     */
    @Override
    public Pager listByPager(@RequestBody ShareholderDto dto) {

        return super.selectPage(dto);
    }
    /**
     * @Method [query]
     * @Description [基础信息页面查询展示]
     * @Author [yangruidong]
     * @Version 1.0
     * @Param matterId 事项ID taxpayersInfoId 纳税主体
     * @Return
     * @Exception
     * @Date
     */
    @Override
    public BaseMessageDto query(@RequestParam("matterId") String matterId, @RequestParam("taxpayersInfoId") String taxpayersInfoId) {
        BaseMessageDto dto = new BaseMessageDto();
        dto.setMatterId(matterId);
        dto = getBaseMessage(dto);
        ShareholderDto shareholderDto = new ShareholderDto();
        List<ShareholderDto> shareholderList = new ArrayList<ShareholderDto>();

        //每次打开先删除该事项的相关纳税主体(暂未处理 根据事项去查看是否提交，提交不同步纳税主体信息)
        baseMessageMapper.deleteShareholder(matterId);

        if(dto==null){
            dto = new BaseMessageDto();
            dto.setBaseId(IdGen.getId());
            dto.setMatterId(matterId);
            try {
                save(dto);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //重新插入
        shareholderDto.setTaxpayersinfo(taxpayersInfoId);
        shareholderList= baseMessageMapper.listByCondition(shareholderDto);
        try{
            for(int i=0;i<shareholderList.size();i++){
                shareholderDto = shareholderList.get(i);
                shareholderDto.setId(IdGen.getId());
                shareholderDto.setTaxpayersinfo(taxpayersInfoId);
                shareholderDto.setBaseId(dto.getBaseId());
                shareholderDto.setDelmark("0");
                baseMessageMapper.insetShareholder(shareholderDto);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        dto.setShareholder(shareholderList);
        return dto;
    }
    /**
     * @Method    listByCondition
     * @Param
     * @Return
     * @Exception
     * @Description [根据条件查询查询]
     * @Author      yangruidong
     * @Version     1.0
     * @Date        2018/5/24 16:50
     */
    @Override
    public List<ShareholderDto> listByCondition(@RequestBody ShareholderDto dto) {
        return baseMessageMapper.listByCondition(dto);
    }
    /**
     * @Method      updateShareholder
     * @Param
     * @Return
     * @Exception
     * @Description [更新股东信息]
     * @Author      yangruidong
     * @Version     1.0
     * @Date        2018/5/24 17:05
     */
    @Override
    public void updateShareholder(@RequestBody ShareholderDto dto) {
        baseMessageMapper.updateShareholder(dto);
    }


    /**
     * @Method      \insetShareholder
     * @Description 插入股东信息
     * @Author      [yangruidong]
     * @Version     1.0
     * @Param
     * @Return
     * @Exception
     * @Date        2018/1/10 17:14
     */
    @Override
    public void insetShareholder(@RequestBody ShareholderDto dto) {

        //通过纳税主体查询股东信息

        baseMessageMapper.insetShareholder(dto);
    }
    /**
     * @Method      ListShareholder
     * @Description 查询股东信息
     * @Author      [yangruidong]
     * @Version     1.0
     * @Param
     * @Return
     * @Exception
     * @Date        2018/1/10 17:14
     */
    @Override
    public List<ShareholderDto> ListShareholder(@RequestBody ShareholderDto dto) {

        return baseMessageMapper.ListShareholder(dto);
    }
    /**
     * @Method      getBaseMessage
     * @Description 获取基础信息
     * @Author      [yangruidong]
     * @Version     1.0
     * @Param
     * @Return
     * @Exception
     * @Date        2018/1/10 17:14
     */
    @Override
    public BaseMessageDto getBaseMessage(@RequestBody BaseMessageDto dto) {
        return  baseMessageMapper.getBaseMessage(dto);
    }


}
